package edu.chalmers;

import java.util.HashMap;
import java.util.HashSet;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class AddActivity extends SherlockActivity{
	private SharedBookManager bookManager = SharedBookManager.getBookManager();
	
	@Override
	public void onCreate(Bundle icicle){
		super.onCreate(icicle);
		setContentView(R.layout.add);
		
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowTitleEnabled(false);
	}
   
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.layout.add_menu, menu);
		return true;
	}
   
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
//		switch (item.getItemId()) {
//		case android.R.id.home:
		if(id == android.R.id.home){
			setResult(RESULT_OK);
			finish();
			return true;
		} else if(id == R.id.menu_saveBook){
//	   	case R.id.menu_saveBook:
	   		String newAuthor = ((EditText) findViewById(R.id.editAuthor)).getText().toString();
	   		String newTitle = ((EditText) findViewById(R.id.editTitle)).getText().toString();
	   		String newIsbn = ((EditText) findViewById(R.id.editIsbn)).getText().toString();
	   		String newCoruse = ((EditText) findViewById(R.id.editCourse)).getText().toString();
	   		int newPrice = 0;
	   		if(!((EditText) findViewById(R.id.editPrice)).getText().toString().equals("")){
	   			newPrice = Integer.parseInt(((EditText) findViewById(R.id.editPrice)).getText().toString());
	   		}
	   		bookManager.addBook(bookManager.createBook(newAuthor, newTitle, newIsbn, newCoruse, newPrice));
	   		saveBooks();
	   		setResult(RESULT_OK);
	   		finish();
	   		return true;
	   } else{
//	   	default:
   			return super.onOptionsItemSelected(item);
	   }
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
	
	private void saveBooks(){
		HashSet<String> header = new HashSet<String>();
		HashMap<String, HashSet<String>> books = new HashMap<String, HashSet<String>>();
		
		SharedPreferences settings = getSharedPreferences("prefs", 0);
		SharedPreferences.Editor editor = settings.edit();
		
		for(Book book : bookManager.allBooks()){
			header.add(book.getTitle());
			HashSet<String> bookData = new HashSet<String>();
			bookData.add(""+book.getAuthor());
			bookData.add(""+book.getTitle());
			bookData.add(""+book.getIsbn());
			bookData.add(""+book.getCourse());
			bookData.add(""+book.getPrice());
			editor.putStringSet(book.getTitle(), bookData);
		}
		editor.putStringSet("header", header);
		editor.commit();
	}
}
